package com.byxy.shop.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.shop.dao.GoodsInfoDao;
import com.byxy.shop.dao.ShoppingCartItemDao;
import com.byxy.shop.entity.GoodsInfo;
import com.byxy.shop.entity.ShoppingCartItem;
import com.byxy.shop.service.MyShoppingCartService;
import com.byxy.ssm.shop.vo.MyShoppingCartItemVO;

@Service
@Transactional
public class MyShoppingCartServiceImpl implements MyShoppingCartService {

	@Autowired
	private ShoppingCartItemDao shoppingCartItemDao;
	@Autowired
	private GoodsInfoDao goodsInfoDao;
	
	@Override
	public List<MyShoppingCartItemVO> getMyShoppingCartItems(Integer userId) {
		List<MyShoppingCartItemVO> myShoppingCartItemVOS = new ArrayList<>();
		List<ShoppingCartItem> myShoppingCartItems = shoppingCartItemDao.selectByUserId(userId);
		for(ShoppingCartItem shoppingCart : myShoppingCartItems) {
			MyShoppingCartItemVO cart = new MyShoppingCartItemVO();
			BeanUtils.copyProperties(shoppingCart,cart);
			GoodsInfo goodsInfo = goodsInfoDao.getById(shoppingCart.getGoodsId());
			cart.setGoodsName(goodsInfo.getGoodsName());
			cart.setGoodsCoverImg(goodsInfo.getGoodsCoverImg());
			cart.setSellingPrice(goodsInfo.getSellingPrice());
			myShoppingCartItemVOS.add(cart);
		}
		return myShoppingCartItemVOS;
	}

	@Override
	public Integer cartCount() {
		// TODO Auto-generated method stub
		return shoppingCartItemDao.cartCount();
	}

	@Override
	public ShoppingCartItem getById(Integer goodsId, Integer cartItemId) {
		// TODO Auto-generated method stub
		return shoppingCartItemDao.selectByUserIdAndGoodsId(goodsId, cartItemId);
	}

	@Override
	public int save(Integer goodsId, Integer goodsCount, Integer userId) {
		ShoppingCartItem cartItem = new ShoppingCartItem();
		cartItem.setGoodsId(goodsId);
        cartItem.setUserId(userId);
        cartItem.setGoodsCount(goodsCount);
        cartItem.setIsDeleted(0);
        cartItem.setCreateTime(new Date());
        cartItem.setUpdateTime(new Date());
        Integer car = shoppingCartItemDao.save(cartItem);
		return car;
	}

	@Override
	public int updateCart(ShoppingCartItem sct) {
		// TODO Auto-generated method stub
		return shoppingCartItemDao.updateCart(sct);
	}

	@Override
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return shoppingCartItemDao.deleteById(id);
	}


}
